package com.example.recruitment.controller;

import com.example.recruitment.entity.User;
import com.example.recruitment.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class RegisterController {

    @Autowired
    private UserService userService;

    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }

    @PostMapping("/register")
    @ResponseBody
    public String register(@RequestParam String username,
                          @RequestParam String password,
                          @RequestParam String realName,
                          @RequestParam String phone,
                          @RequestParam String email,
                          @RequestParam String role) {
        // 检查用户名是否已存在
        if (userService.findByUsername(username) != null) {
            return "用户名已存在";
        }

        // 创建新用户
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRealName(realName);
        user.setPhone(phone);
        user.setEmail(email);
        user.setRole(role);

        // 保存用户
        if (userService.add(user)) {
            return "success";
        }
        return "注册失败，请稍后重试";
    }
} 